<?
   	try{
            	$bdd = mysqli_connect('localhost', 'root', '', 'cappuccinoo');
				}
				catch(Exception $e){
					 die('Erreur : '.$e->getMessage());
				}
				$post_ok = True;
				if(isset($_POST['submit'])){
            	$name = $_POST['name'];
           	 	$pseudo = strtolower($_POST['pseudo']);
            	$email = $_POST['email'];
            	$password = $_POST['password'];
            	$password_confirm = $_POST['password_confirm'];
				
				$query_pseudo = "SELECT pseudo FROM user WHERE pseudo = '".$pseudo."' ";
            	$result_pseudo = mysqli_query($bdd, $query_pseudo) or die(mysqli_error($bdd));
            	$data_pseudo = mysqli_fetch_array($result_pseudo);
				if($data_pseudo != null) {
                	echo('<div><p>Ce pseudo est déjà utilisé !</p></div>');
                	$post_ok = False;
				}
				
				$pseudo_lenth = strlen($pseudo);
            	if($pseudo_lenth > 25) {
                	echo('<div class="form_error"><p>Le pseudo doit faire moins de 25 caractères !</p></div>');
                	$post_ok = False;
            	}
				
				$query_mail = "SELECT email FROM user WHERE email = '".$email."' ";
            	$result_mail = mysqli_query($bdd, $query_mail) or die(mysqli_error($bdd));
            	$data_mail = mysqli_fetch_array($result_mail);
            	if($data_mail != null) {
                	echo('<div class="form_error"><p>Cette adresse mail est déjà utilisée !</p></div>');
                	$post_ok = False;
            	}
				if($password != $password_confirm){
					echo "Les deux mots de passe sont différents BIATCH !";
					$post_ok = False;
				}
				
				if ($post_ok == True){
					$query = "INSERT INTO user (name, pseudo, email, password)
               		VALUES ('".$name."', '".$pseudo."', '".$email."', '".sha1($password)."');";
                	$result = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
					echo("<script>alert('Oui Monsieur ! Vous etes bien inscrit ! Redirection, Zouuu !')</script>");
					echo('<script>document.location.replace("index.php");</script>');
				}
		}
?>